package com.config;

import java.io.IOException;
import java.util.List;
import java.util.Properties;
import com.job.FirstJob;
import net.sf.jabb.quartz.AutowiringSpringBeanJobFactory;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.simpl.SimpleJobFactory;
import org.quartz.spi.JobFactory;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;

/**
 * @author HuangSuip
 */
//@Configuration
@ComponentScan({"com.job", "com.mapper"})
public class QuartzConfig {

    private List<Trigger> triggers;

    @Autowired
    private FirstJob firstJob;

    public QuartzConfig(FirstJob firstJob) throws IOException, SchedulerException {
/*        triggers = new ArrayList<>();
        CronTriggerFactoryBean bean = new BaseCronTrigger() {
            @Override
            public String getMyCronExpression() {
                return "*//*1 * * * * *";
            }

            @Override
            public String getGroupName() {
                return "Group name";
            }

            @Override
            public String getName() {
                return "firstJob";
            }

            @Override
            public Class<? extends Job> getMyTargetObject() {
                System.out.println("-------------------");
                return FirstJob.class;
            }
        };

        triggers.add(bean.getObject());*/
        //this.schedulerFactoryBean();
        this.firstJob = firstJob;
    }

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(
            @Qualifier("jobFactory") JobFactory jobFactory
    ) throws IOException, SchedulerException {
/*
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        JobDetail jobDetail = JobBuilder.newJob(ScheduledJob.class) .withIdentity("job1", "group1").build();
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1") .withSchedule(scheduleBuilder).build();
        scheduler.scheduleJob(jobDetail,cronTrigger);
        */
/*        CronTriggerFactoryBean bean = new BaseCronTrigger() {
            @Override
            public String getMyCronExpression() {
                return "*//*1 * * * * *";
            }

            @Override
            public String getGroupName() {
                return "Group name";
            }

            @Override
            public String getName() {
                return "firstJob";
            }

            @Override
            public Class<? extends Job> getMyTargetObject() {
                System.out.println("-------------------");
                return FirstJob.class;
            }
        };*/

/*

        */
        //factory.setJobFactory(jobFactory);

        /*JobDetail jobDetail = JobBuilder.newJob(FirstJob.class).withIdentity("First Job","Group job").storeDurably(true).requestRecovery(true).build();
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("*//*5 * * * * ?");
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("Trigger name","Group name").withSchedule(scheduleBuilder).forJob(jobDetail).build();*/
        //factory.setTriggers(cronTrigger);
/*
        Scheduler scheduler = factory.getScheduler();
        System.out.println(scheduler);
*/

/*        SchedulerFactory sf = new StdSchedulerFactory(quartzProperties("/config/quartz-dev.properties"));
        Scheduler sched = sf.getScheduler();*/
/*        if (jobDetail.isDurable()){
            sched.scheduleJob(jobDetail,cronTrigger);
        }*/
/*        sched.deleteJob(jobDetail.getKey());
        sched.scheduleJob(jobDetail, cronTrigger);
        if (jobDetail.isDurable()) {

        }
        sched.start();*/
        //scheduler.scheduleJob(jobDetail,cronTrigger);
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler scheduler = sf.getScheduler();
        //scheduler.scheduleJob(firstJob.getJobDetail(), firstJob.getTrigger());
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        factory.setJobFactory(jobFactory);
        factory.setOverwriteExistingJobs(true);
        factory.setStartupDelay(3);
        factory.setQuartzProperties(quartzProperties("/config/quartz-dev.properties"));
        factory.setApplicationContextSchedulerContextKey("applicationContext");
        factory.setAutoStartup(true);
        factory.setSchedulerFactoryClass(StdSchedulerFactory.class);
        factory.setTriggers(firstJob.getTrigger());
        factory.setJobDetails(firstJob.getJobDetail());

        return factory;
    }

    private Properties quartzProperties(String propsFile) throws IOException {
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        propertiesFactoryBean.setLocation(new ClassPathResource(propsFile));
        propertiesFactoryBean.afterPropertiesSet();
        return propertiesFactoryBean.getObject();
    }

    @Bean(name = "jobFactory")
    public JobFactory jobFactory(ApplicationContext con) {
        AutowiringSpringBeanJobFactory as = new AutowiringSpringBeanJobFactory() {
            @Override
            protected Object createJobInstance(TriggerFiredBundle bundle) {
                return getApplicationContext().getBean(bundle.getJobDetail().getJobClass());
            }
        };

        SimpleJobFactory sjm = new SimpleJobFactory() {
            @Override
            public Job newJob(TriggerFiredBundle bundle, Scheduler Scheduler) {
                return con.getBean(bundle.getJobDetail().getJobClass());
            }
        };
        AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
        jobFactory.setApplicationContext(con);

        SpringBeanJobFactory sbjf = new SpringBeanJobFactory(){
            @Override
            public Job newJob(TriggerFiredBundle bundle, Scheduler scheduler) throws SchedulerException {
                try {
                    return con.getBean(bundle.getJobDetail().getJobClass());
                }
                catch (Exception ex) {
                    throw new SchedulerException("Job instantiation failed", ex);
                }
            }
        };
        return sbjf;
    }
}

class MyJobFactory extends AutowiringSpringBeanJobFactory {


}
